/**
 * Created by Administrator on 2016/2/20.
 */
$(function () {
    var activityDetailInfoDivTpl = _.template($("#tpl_activityDetailInfoDiv").html());

    function init() {
        if (config.debug) {
            $("#atvDetailInfoDiv").html(activityDetailInfoDivTpl(localJson.atvList[0]));
            config.manageActivity.atvInfo = localJson.atvList[0];
            return;
        }
        $.ajax({
            url: CK.formatUrl(config.service.getActivityById),
            data: {
                atvId: config.atvId
            },
            type: "get",
            async: "true",
            success: function (response) {
                if (response.code != AjaxCode.success) {
                    CK.alert(response.msg);
                    return;
                }
                $("#activityContainer").html(activityDetailInfoDivTpl(response.data.atvData));
                config.manageActivity.atvInfo = response.data.atvData;
            },
            error: function () {

            }
        });
    }

    init();
    //对服务器的数据进行处理
    function formatData(data) {
        if (!data) {
            return;
        }
        var dataReturn = data;
        if (data.atvStartTime) {
            var iniDate = data.atvStartTime;
            dataReturn.dateStart = iniDate.substr(0, iniDate.lastIndexOf(' '));
            dataReturn.timeStart = iniDate.substr(iniDate.lastIndexOf(' ') + 1, iniDate.length);
        }

        if (data.atvEndTime) {
            var iniDate = data.atvEndTime;
            dataReturn.dateEnd = iniDate.substr(0, iniDate.lastIndexOf(' '));
            dataReturn.timeEnd = iniDate.substr(iniDate.lastIndexOf(' ') + 1, iniDate.length);
        }
        if (data.signUpEndTime) {
            var iniDate = data.signUpEndTime;
            dataReturn.deadlineDate = iniDate.substr(0, iniDate.lastIndexOf(' '));
            dataReturn.deadlineTime = iniDate.substr(iniDate.lastIndexOf(' ') + 1, iniDate.length);
        }
        return dataReturn;
    }


    $("#editInfo").click(function () {
        var editActivityInfoDivTpl = _.template($("#tpl_editActivityInfoDiv").html());
        $("#atvDetailInfoDiv").html(editActivityInfoDivTpl(formatData(config.manageActivity.atvInfo)));
        $("#detailTextarea").wangEditor();//输入框初始化
        $(this).addClass("hidden");//按钮消失
        $("#editCmp").removeClass("hidden");
    });

    $("#atvDetailInfoDiv").delegate("[forEdit]", "input propertychange", function () {
        $("#editCmp").find("input").removeAttr("disabled");
    });

    //隐藏详细信息
    $("#atvDetailInfoDiv").delegate("#expendDetail", "click", function () {
        if ($("#atvDetailInfo").hasClass("hidden")) {
            $("#atvDetailInfo").removeClass("hidden");
        } else {
            $("#atvDetailInfo").addClass("hidden");
        }
    });

    //点击完成按钮
    $("#editCmp").click(function () {
        var data = config.manageActivity.atvInfo;
        $("#atvDetailInfoDiv").find("[forEdit]").each(function () {
            var $this = $(this);
            data[$this.attr("name")] = $this.val();
        });

        config.manageActivity.atvInfo = data;
        if (config.debug) {
            return;
        }
        $.ajax({
            url: CK.formatUrl(config.service.modifyActivityById),
            data: data,
            type: "get",
            async: "true",
            success: function (response) {
                if (response.code != AjaxCode.success) {
                    CK.alert(response.msg);
                    return;
                }
            },
            error: function () {

            }
        });
    })
});
